Strict

Import agfx

Global myTmpScreen:TTmpScreen

Class TAgfx_test_app Extends TFramework

	Method OnCreate:Int()
		
		Super.OnCreate()
				
		DISPLAY.SetVirtualSize(800,600)
		
		myTmpScreen = New TTmpScreen		
		myTmpScreen.Prepare()
		
		Return 0
	End
	
End


Class TTmpScreen Extends TScreen
	
	Field polygons:ArrayList<TPrimitive>
	Field polygon1:TPolygon
	Field polygon2:TPolygon
	Field polygon3:TPolygon
	Field triangle:TTriangle

	
	Field velocity:TVector2 = New TVector2(0,0)
	Field playerTransition:TVector2 = New TVector2()
	Field coll:TCollisionResult = New TCollisionResult()
	Field a:Float
	' START when app is created
	Method Start:Void()
	
		
		Self.polygons = New ArrayList<TPrimitive>()
	
		' POLYGON 1
		Self.polygon1 = New TPolygon()

		Self.polygon1.AddPoint(New TVector2(0,0))
		Self.polygon1.AddPoint(New TVector2(32,0))
		Self.polygon1.AddPoint(New TVector2(32,32))
		Self.polygon1.AddPoint(New TVector2(0,32))
		Self.polygon1.Rotate(45)
		Self.polygon1.SetOffset(64,128)
		Self.polygon1.Update()
		
		Self.polygons.Add(Self.polygon1)
		
		
		' POLYGON 2
		Self.polygon2 = New TPolygon()
		
		Self.polygon2.AddPoint(New TVector2(50,50))
		Self.polygon2.AddPoint(New TVector2(100,0))
		Self.polygon2.AddPoint(New TVector2(150,150))
		Self.polygon2.SetOffset(80,80)
		
		Self.polygons.Add(Self.polygon2)
		
		' POLYGON 3
		Self.polygon3 = New TPolygon()
		
		Self.polygon3.AddPoint(New TVector2(0,50))
		Self.polygon3.AddPoint(New TVector2(50,0))
		Self.polygon3.AddPoint(New TVector2(150,80))
		Self.polygon3.AddPoint(New TVector2(160,200))
		Self.polygon3.AddPoint(New TVector2(-10,190))
		Self.polygon3.SetOffset(300,300)
		
		Self.polygons.Add(Self.polygon3)
		
		
		Self.triangle = New TTriangle()
		
				' CREATE TRIANGLE
		Self.triangle = New TTriangle()
		Self.triangle.Create(New TVector2(-25,0),New TVector2(0,50),New TVector2(25,0))		
		Self.triangle.SetOffset(150,300)
		
		Self.polygons.Add(Self.triangle)
		
		
	End
	
	' main app logic loop update
	Method Update:Void()
		
		
		Self.velocity.Set(0,0)
		
		If KeyDown(KEY_UP) Then Self.velocity.Set(0,-3)
		If KeyDown(KEY_DOWN) Then Self.velocity.Set(0,3)
		If KeyDown(KEY_LEFT) Then Self.velocity.Set(-3,0)
		If KeyDown(KEY_RIGHT) Then Self.velocity.Set(3,0)
		
		Local was_collision_hit:Bool = False
		
		playerTransition.Set(velocity)
		
		For Local pl:TPrimitive = EachIn Self.polygons
			
			If Not was_collision_hit Then
				If Self.polygon1<>pl Then
				
					coll = COLLISION.DetectPrimitiveCollision(Self.polygon1,pl,Self.velocity)
				
					If (coll.WillIntersect) Then 
						was_collision_hit = True
						playerTransition = VecAdd(Self.velocity,coll.MinimumTranslationVector)						
					Else
						playerTransition = Self.velocity	
					EndIf
				EndIf
			End If
				
		Next
		
		Self.polygon1.SetOffset(playerTransition)
		
		For Local pol:TPrimitive = EachIn Self.polygons
			pol.Update()
		Next		
		
		Self.triangle.Rotate(0.125)
		
	End
	
	' main app render loop
	Method Render:Void()
		
		Cls
	
		For Local pl:TPrimitive = EachIn Self.polygons
			pl.DrawDebug()
		Next
		
		Local str_b:String 
		If coll.Intersect Then str_b="True" Else str_b = "False"
		DrawText("Intersect      :"+str_b,10,20)
		If coll.WillIntersect Then str_b="True" Else str_b = "False"
		DrawText("Willntersect   :"+str_b,10,40)		
		DrawText("min Transiiton :"+coll.MinimumTranslationVector.ToString(),10,60)
		
		
	
		
		
	End
End
